﻿Imports Training.Common

Partial Public Class Training
    Inherits System.Web.UI.Page
    Public gname As String ' groupname
    Dim usr As MembershipUser = Membership.GetUser()

    ''' <summary>
    ''' Page load event. Check for role "user" and get GroupID and UserID
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>If role is NOT user redirect out</remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not User.IsInRole("user") Then
            Response.Redirect("login.aspx")
        Else
            ' get GroupID and UserID
            GetIDs()
            ' set group name or user name
            If CType(Session("gid"), Integer) <> 1 Then
                gname = Session("gname")
            Else
                gname = usr.UserName.ToString
            End If

            ' set logo if available
            If Not IsNothing(Session("logo")) Then
                imgLogo.Visible = True
                imgLogo.ImageUrl = "images/logos/" & Session("logo")
            End If

            ' load modules for group
            rptModules.DataSource = GetModulesForGroup(Session("gid"))
            rptModules.DataBind()

        End If
    End Sub

    ''' <summary>
    ''' Clear the session on loguot
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub LoginStatus2_LoggedOut(ByVal sender As Object, ByVal e As EventArgs)
        Session.Abandon()
    End Sub

    ''' <summary>
    ''' Use logged in users UserID to load UserID,GroupID and GroupName into variables
    ''' </summary>
    ''' <remarks>set session variables for use in other code</remarks>
    Private Sub GetIDs()
        ' lookup userid for user to associate login with user table
        Dim uid As String = usr.ProviderUserKey.ToString()

        ' call sub to load public variables
        GetUserGroupInfo(uid)

        ' set session variables for info
        Session("uid") = aUserID
        Session("gid") = groupid
        Session("gname") = groupname
        Session("logo") = grouplogo
    End Sub

    ''' <summary>
    ''' Loop through Datatable of Quizzes and display link to page that will run quiz
    ''' </summary>
    ''' <param name="mdid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ModuleQuizzes(ByVal mdid As Integer) As String
        Dim retVal As String
        Dim dTable As DataTable = GetModuleQuizzes(mdid)
        Dim uVars As String


        retVal = String.Empty

        For Each row As DataRow In dTable.Rows

            ' check for completed
            If CheckCompletedQuizForUser(Session("uid"), row("QuizID")) Then
                Dim uPass As Boolean ' check if user passed quiz
                uPass = CheckUserQuizPassing(Session("uid"), row("QuizID"))

                ' display ability to print certificate if user passed quiz
                If uPass Then
                    uVars = "uid=" & Session("uid") & "&qid=" & row("QuizID")

                    retVal &= "* <i>" & row("QuizName") & "</i> " & _
                        "<img src=""images/quizComplete.gif"" alt=""complete"" border=""0"" /> " & _
                        "<a href=""CertPrint.aspx?data=" & Server.UrlEncode(Encrypt(uVars)) & _
                        """ target=""_blank""><img src=""images/printCert.gif"" alt=""print certificate"" border=""0"" /></a>" & _
                        "<a href=""RetakeQuiz.aspx?qid=" & _
                                row("QuizID") & "&qnm=" & row("QuizName") & """><img src=""images/retake.gif"" alt=""retake"" border=""0"" /></a><br />"
                Else
                    retVal &= "* <i>" & row("QuizName") & "</i> <a href=""RetakeQuiz.aspx?qid=" & _
                                row("QuizID") & "&qnm=" & row("QuizName") & """><img src=""images/reload.gif"" alt=""retake"" border=""0"" /></a><br />"
                End If

            Else
                retVal &= "* <a href=""Quiz.aspx?qid=" & _
                                row("QuizID") & "&qnm=" & row("QuizName") & """>" & _
                                    row("QuizName") & "</a><br />"
            End If


        Next

        Return retVal
    End Function

    ''' <summary>
    ''' Loop through Datatable of videos for module, show video names and links to file formats
    ''' </summary>
    ''' <param name="mdid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ModuleVideos(ByVal mdid As Integer) As String
        Dim retVal As String
        Dim dTable As DataTable = GetModuleVideos(mdid)

        retVal = String.Empty

        For Each row As DataRow In dTable.Rows
            'retVal &= "* " & row("VideoName") & "<br />" & VideoFiles(row("VideoID"))
            retVal &= VideoFiles(row("VideoID"))
        Next

        Return retVal
    End Function

    ''' <summary>
    ''' Loop through datatabele of video files for display link to view
    ''' </summary>
    ''' <param name="vid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function VideoFiles(ByVal vid As Integer) As String
        Dim retVal As String
        Dim vTable As DataTable = GetVideoFilesForVideo(vid)

        retVal = String.Empty

        For Each row As DataRow In vTable.Rows
            retVal &= "* <a href=""Video.aspx?vif=" & row("VideoFileID") & """ target=""_blank"">" & _
                row("Description") & "</a><br />"
        Next

        Return retVal
    End Function

    ' ISN'T IT TIME FOR....
    '              .-. 
    '        .-'``(|||) 
    '     ,`\ \    `-`.               88                         88 
    '    /   \ '``-.   `              88                         88 
    '  .-.  ,       `___:    88   88  88,888,  88   88  ,88888, 88888  88   88 
    ' (:::) :        ___     88   88  88   88  88   88  88   88  88    88   88 
    '  `-`  `       ,   :    88   88  88   88  88   88  88   88  88    88   88 
    '    \   / ,..-`   ,     88   88  88   88  88   88  88   88  88    88   88 
    '     `./ /    .-.`      '88888'  '88888'  '88888'  88   88  '8888 '88888' 
    '       `-..-(   ) 
    '              `-` 

End Class